﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Purchases;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class GoodsReceivingReport : RibbonForm
    {
        public GoodsReceivingReport()
        {
            InitializeComponent();
        }

        #region Properties
        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }
        public bool IsMultipleWarehouse { get; set; }
        public string DefaultWarehouse { get; set; }

        public DataTable GRPOLineItems { get; set; }
        public DataTable AccountingEntries { get; set; }
        public string DefaultPriceList { get; set; }
        public string DefaultCurrency { get; set; }
        public double WithHoldingTaxRate { get; set; }
        public string VATCode { get; set; }
        public string VATRate { get; set; }
        public bool IsInclusive { get; set; }


        //txtCode
        private string _CardCode;

        public string CardCode
        {
            get { return _CardCode; }
            set { _CardCode = value; txtCardCode.Text = _CardCode; }
        }
        private string _CardName;

        public string CardName
        {
            get { return _CardName; }
            set { _CardName = value; txtCardName.Text = _CardName; }
        }
        private string _Warehouse;

        public string Warehouse
        {
            get { return _Warehouse; }
            set { _Warehouse = value; cboLocation.SelectedValue = _Warehouse; }
        }
        private string _ContactPerson;

        public string ContactPerson
        {
            get { return _ContactPerson; }
            set { _ContactPerson = value; txtContactPerson.Text = _ContactPerson; }
        }
        private string _Reference;

        public string Reference
        {
            get { return _Reference; }
            set { _Reference = value; txtReference.Text = _Reference; txtReference.Properties.ReadOnly = true; }
        }
        private string _BillTo;

        public string BillTo
        {
            get { return _BillTo; }
            set { _BillTo = value; txtBillTo.Text = _BillTo; }
        }
        private string _ShipTo;

        public string ShipTo
        {
            get { return _ShipTo; }
            set { _ShipTo = value; txtShipTo.Text = _ShipTo; }
        }
        private DateTime _PostingDate;

        public DateTime PostingDate
        {
            get { return _PostingDate; }
            set { _PostingDate = value; dtPostDate.EditValue = _PostingDate; }
        }
        private DateTime _DeliveryDate;

        public DateTime DeliveryDate
        {
            get { return _DeliveryDate; }
            set { _DeliveryDate = value; dtDeliveryDate.EditValue = _DeliveryDate; }
        }
        private DateTime _DocumentDate;

        public DateTime DocumentDate
        {
            get { return _DocumentDate; }
            set { _DocumentDate = value; dtDocDate.EditValue = _DocumentDate; }
        }

        private string _Remarks;

        public string Remarks
        {
            get { return _Remarks; }
            set { _Remarks = value; txtRemarks.Text = _Remarks; }
        }
        private string _GrossAmount;

        public string GrossAmount
        {
            get { return _GrossAmount; }
            set { _GrossAmount = value; txtGrossAmount.Text = _GrossAmount; }
        }
        private string _TotalLineDiscount;

        public string TotalLineDiscount
        {
            get { return _TotalLineDiscount; }
            set { _TotalLineDiscount = value; txtTotalLineDiscount.Text = _TotalLineDiscount; }
        }
        private string _Disc1;

        public string Disc1
        {
            get { return _Disc1; }
            set { _Disc1 = value; txtDisc1.Text = _Disc1; }
        }
        private string _Disc2;

        public string Disc2
        {
            get { return _Disc2; }
            set { _Disc2 = value; txtDisc2.Text = _Disc2; }
        }
        private string _Disc3;

        public string Disc3
        {
            get { return _Disc3; }
            set { _Disc3 = value; txtDisc3.Text = _Disc3; }
        }
        private string _Disc4;

        public string Disc4
        {
            get { return _Disc4; }
            set { _Disc4 = value; txtDisc4.Text = _Disc4; }
        }

        private string _TotalDiscount;

        public string TotalVolumeDiscount
        {
            get { return _TotalDiscount; }
            set { _TotalDiscount = value; txtTotalDiscount.Text = _TotalDiscount; }
        }
        private string _VAT;

        public string VAT
        {
            get { return _VAT; }
            set { _VAT = value; txtVAT.Text = _VAT; }
        }
        private string _NetAmount;

        public string NetAmount
        {
            get { return _NetAmount; }
            set { _NetAmount = value; txtNetAmt.Text = _NetAmount; }
        }


        private string _RequestedBy;

        public string RequestedBy
        {
            get { return _RequestedBy; }
            set { _RequestedBy = value; txtRequestedBy.Text = _RequestedBy; }
        }


        private string _Department;

        public string Department
        {
            get { return _Department; }
            set { _Department = value; txtDepartment.Text = _Department; }
        }

        #endregion

        #region Initialization

        private static DataTable GetGLAcctStructure()
        {
            DataTable table = new DataTable();

            table.Columns.Add("TransDate", typeof(DateTime));
            table.Columns.Add("TransType", typeof(string));
            table.Columns.Add("TransNum", typeof(string));

            table.Columns.Add("AcctCode", typeof(string));
            table.Columns.Add("AcctName", typeof(string));
            table.Columns.Add("Particulars", typeof(string));
            table.Columns.Add("DebitAmt", typeof(double));
            table.Columns.Add("CreditAmt", typeof(double));

            return table;
        }

        private static DataTable GetTable()
        {
            DataTable table = new DataTable();

            table.Columns.Add("ItemCode", typeof(string));
            table.Columns.Add("ItemDesc", typeof(string));
            table.Columns.Add("UoM", typeof(string));
            table.Columns.Add("Qty", typeof(double));
            table.Columns.Add("LotNumber", typeof(string));
            table.Columns.Add("MfrDate", typeof(DateTime));
            table.Columns.Add("ExpiryDate", typeof(DateTime));
            table.Columns.Add("UnitPrice", typeof(double));
            table.Columns.Add("IsFree", typeof(bool));
            table.Columns.Add("DiscPrcnt", typeof(double));
            table.Columns.Add("Discount", typeof(double));
            table.Columns.Add("TotalLineDisc", typeof(double));
            table.Columns.Add("Amount", typeof(double));
            table.Columns.Add("ItemGuid", typeof(double));
            
            return table;

        }

        void InitializeSeries()
        {

            string _GRPO_SERIES = "8";
            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();

            lblValidUntilDeliveryDate.Text = "Date Needed:";
            if (oSeries.IsAutoNumber(_GRPO_SERIES) == true)
            {
                txtDocumentNumber.Properties.ReadOnly = true;
                txtDocumentNumber.Text = "##########";

            }
            oSeries.Close();
 
        }
        
        #endregion

        #region Functions

        public string GetDiscountMethod()
        {
            string sPath = System.Windows.Forms.Application.StartupPath.ToString() + "\\config.conf";
            string _OUTPUT_STRING = "";


            if (File.Exists(sPath) == true)
            {


                StreamReader sr = File.OpenText(sPath);
                string input;

                do
                {
                    input = sr.ReadLine();
                    if (input != null)
                    {

                        if (input.StartsWith("DiscountMethod") == true)
                        {
                            int iStart = input.IndexOf("=");
                            _OUTPUT_STRING = input.Substring(iStart + 1);

                            break;
                        }

                    }
                } while (input != null);


            }
            return _OUTPUT_STRING;

        }

        void RemoveBlankLastGLAccounts()
        {

            int iRowIndex = gridViewRRAcctEntries.RowCount;
            gridViewRRAcctEntries.FocusedRowHandle = iRowIndex - 1;
            try
            {
                string _AcctCode = "";


                if (gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]) != null)
                {
                    _AcctCode = gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]).ToString();
                }
                else
                {
                    _AcctCode = "";
                }

                if (_AcctCode.Trim() == "")
                {
                    gridViewRRAcctEntries.DeleteRow(gridViewRRAcctEntries.FocusedRowHandle);
                }
            }
            catch { }

        }

        public void GetBusinessPartnerDefaults(string sCardCode)
        {
            #region Getting the Default Setup of Customer in BP Master File
            AddressBookManager oBPManager = new AddressBookManager();

            oBPManager.ConnectionString = this.ConnectionString;
            oBPManager.Open();

            this.DefaultPriceList = oBPManager.GetPriceListCode(sCardCode);
            this.WithHoldingTaxRate = oBPManager.GetWithHoldingTaxRate(sCardCode);

            oBPManager.Close();
            #endregion


            //dtDeliveryDate.EditValue = ((DateTime)dtPostDate.EditValue).AddDays(Convert.ToInt16(txtPaymentTerms.Text));

            #region Getting the Tax Code Setup
            TaxManager oTxManager = new TaxManager();

            oTxManager.ConnectionString = this.ConnectionString;
            oTxManager.Open();

            this.VATRate = oTxManager.GetVATRate(this.VATCode).ToString();
            this.IsInclusive = oTxManager.IsInclusive(this.VATCode) == "Y" ? true : false;

            #endregion


        }

        public void PrintPreview()
        {
            PRNT_GRPO oPrint = new PRNT_GRPO();

            oPrint.ConnectionString = this.ConnectionString;
            oPrint.DocumentNumber = txtDocumentNumber.Text;

            oPrint.WindowState = FormWindowState.Maximized;
            oPrint.ShowDialog();
        }
        bool HasLotNumberExists()
        {
            bool _HasLotNumber = false;
            string _Warehouse = cboLocation.SelectedValue.ToString();
            StockManager oManager = new StockManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            foreach (DataRow oRow in this.GRPOLineItems.Rows)
            {
                string _ItemCode = oRow["ItemCode"].ToString();
                string _LotNumber = oRow["LotNumber"].ToString();

                _HasLotNumber = oManager.IsLotNumberExists(_Warehouse, _ItemCode, _LotNumber);

                if (_HasLotNumber == true) { break; }
            }

            return _HasLotNumber;
        }

        void EnableControls()
        {
            if (this.IsMultipleWarehouse == true)
            {
                cboLocation.Enabled = true;
            }
            txtCardCode.Enabled = true;
            txtCardName.Enabled = true;
            txtContactPerson.Enabled = true;
            txtReference.Enabled = true;
            txtBillTo.Enabled = true;
            txtShipTo.Enabled = true;
            txtDocumentNumber.Enabled = true;
            dtPostDate.Enabled = true;
            dtDeliveryDate.Enabled = true;
            dtDocDate.Enabled = true;

            txtRemarks.Enabled = true;
            txtPaymentTerms.Enabled = true;
            txtSuppliersInvoice.Enabled = true;
            txtGrossAmount.Enabled = true;
            txtTotalLineDiscount.Enabled = true;
            txtDisc1.Enabled = true;
            txtDisc2.Enabled = true;
            txtDisc3.Enabled = true;
            txtDisc4.Enabled = true;
            txtTotalDiscount.Enabled = true;
            txtVAT.Enabled = true;
            txtNetAmt.Enabled = true;

            btnAdd.Enabled = true;

            foreach (DevExpress.XtraGrid.Columns.GridColumn oColumn in gridViewRR.Columns)
            {
                if (oColumn.Name == "Code" || oColumn.Name == "Quantity" || oColumn.Name == "Free")
                {
                    oColumn.OptionsColumn.AllowEdit = true;
                }
            }

        }

        void DisableControls()
        {
            cboLocation.Enabled = false;
            txtCardCode.Enabled = false;
            txtCardName.Enabled = false;
            txtContactPerson.Enabled = false;
            txtReference.Enabled = false;
            txtBillTo.Enabled = false;
            txtShipTo.Enabled = false;
            txtDocumentNumber.Enabled = false;
            dtPostDate.Enabled = false;
            dtDeliveryDate.Enabled = false;
            dtDocDate.Enabled = false;

            txtPaymentTerms.Enabled = false;
            txtSuppliersInvoice.Enabled = false;
            txtRemarks.Enabled = false;
            txtGrossAmount.Enabled = false;
            txtTotalLineDiscount.Enabled = false;
            txtDisc1.Enabled = false;
            txtDisc2.Enabled = false;
            txtDisc3.Enabled = false;
            txtDisc4.Enabled = false;
            txtTotalDiscount.Enabled = false;
            txtVAT.Enabled = false;
            txtNetAmt.Enabled = false;

            btnAdd.Enabled = false;
            foreach (DevExpress.XtraGrid.Columns.GridColumn oColumn in gridViewRR.Columns)
            {
                oColumn.OptionsColumn.AllowEdit = false;
            }
            
        }

        string GetNewSeries()
        {
            string _SERIES = "";
            string _DOCTYPE = "8";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_DOCTYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_DOCTYPE);
            }
            else
            {
                _SERIES = txtDocumentNumber.Text;
            }
            oSeries.Close();

            return _SERIES;
        }

        void ComputeTotal()
        {
            try
            {
                double _GrossAmount = 0;
                double _NetAmount = 0;
                double _LineDiscount = 0;
                
                foreach (DataRow oRow in this.GRPOLineItems.Rows)
                {
                    if (oRow["Amount"] != DBNull.Value)
                    {
                        byte IsFree = Convert.ToByte(oRow["IsFree"]);
                        int iIsFree = Convert.ToInt16(IsFree);

                        if (iIsFree == 0)
                        {
                            _GrossAmount = _GrossAmount + Convert.ToDouble(oRow["Qty"]) * Convert.ToDouble(oRow["UnitPrice"]);
                        }
                        
                        _NetAmount = _NetAmount + (Convert.ToDouble(oRow["Amount"]));
                        if (oRow["TotalLineDisc"] == DBNull.Value)
                        {
                            oRow["TotalLineDisc"] = 0;
                        }
                        _LineDiscount = _LineDiscount + (Convert.ToDouble(oRow["TotalLineDisc"]));
                    }
                }

                double _VAT = 0;
                double _VolumeDiscount = 0;




                if (txtTotalDiscount.Text != "")
                {
                    try
                    {
                        _VolumeDiscount = Convert.ToDouble(txtTotalDiscount.Text);
                    }
                    catch
                    {
                        _VolumeDiscount = 0;
                    }
                }


                if (this.VATCode == "VAT Inclusive")
                {
                    if (this.IsInclusive == true)
                    {

                        _VAT = _NetAmount - ((_NetAmount - _VolumeDiscount) / Convert.ToDouble(this.VATRate));
                        _VAT = Convert.ToDouble(_VAT.ToString("##.00"));
                        txtVAT.Text = _VAT.ToString();
                    }
                    else
                    {
                        txtVAT.Text = "0";
                    }
                }

                if (this.VATCode == "VAT Exclusive")
                {
                    if (this.IsInclusive == false)
                    {

                        //_VAT = _NetAmount - ((_NetAmount - _VolumeDiscount) / Convert.ToDouble(this.VATRate));
                        _VAT = _NetAmount * Convert.ToDouble(this.VATRate);
                        _VAT = Convert.ToDouble(_VAT.ToString("##.00"));
                        txtVAT.Text = _VAT.ToString();
                    }
                    else
                    {
                        txtVAT.Text = "0";
                    }
                }

                txtTotalLineDiscount.Text = _LineDiscount.ToString("#,###.00");
                txtGrossAmount.Text = _GrossAmount.ToString("#,###.00");

                txtNetAmt.Text = _NetAmount.ToString("#,###.00");

                ApplyVolumeDiscount();
            }
            catch
            {
            }

        }

        void ApplyVolumeDiscount()
        {

            double _GrossAmount = 0;
            double _NetAmount = 0;
            double _TotalLineDiscount = 0;
            double _VatAmount = 0;


            foreach (DataRow oRow in this.GRPOLineItems.Rows)
            {

                if (oRow["Qty"] == DBNull.Value)
                {
                    oRow["Qty"] = 0;
                }
                if (oRow["UnitPrice"] == DBNull.Value)
                {
                    oRow["UnitPrice"] = 0;
                }
                if (oRow["TotalLineDisc"] == DBNull.Value)
                {
                    oRow["TotalLineDisc"] = 0;
                }
                if (oRow["IsFree"] == DBNull.Value)
                {
                    oRow["IsFree"] = false;
                }
                bool IsFree = Convert.ToBoolean(oRow["IsFree"]);

                if (IsFree != true)
                {
                    _GrossAmount = _GrossAmount + Convert.ToDouble(oRow["Qty"]) * Convert.ToDouble(oRow["UnitPrice"]);
                    _TotalLineDiscount = _TotalLineDiscount + Convert.ToDouble(oRow["TotalLineDisc"]);
                }

            }

            txtDisc1.Text = txtDisc1.Text.Replace("%", "");
            txtDisc2.Text = txtDisc2.Text.Replace("%", "");
            txtDisc3.Text = txtDisc3.Text.Replace("%", "");
            txtDisc4.Text = txtDisc4.Text.Replace("%", "");


            string sDiscount1 = txtDisc1.Text.Replace("%", "");
            string sDiscount2 = txtDisc2.Text.Replace("%", "");
            string sDiscount3 = txtDisc3.Text.Replace("%", "");
            string sDiscount4 = txtDisc4.Text.Replace("%", "");


            double _disc1 = 0;
            double _disc2 = 0;
            double _disc3 = 0;
            double _disc4 = 0;


            _disc1 = Convert.ToDouble(sDiscount1);
            _disc2 = Convert.ToDouble(sDiscount2);
            _disc3 = Convert.ToDouble(sDiscount3);

            double _TotalDiscount = 0;

            double _discount1 = 0;
            double _discount2 = 0;
            double _discount3 = 0;
            double _discount4 = 0;

            _disc1 = Convert.ToDouble(sDiscount1);
            _disc2 = Convert.ToDouble(sDiscount2);
            _disc3 = Convert.ToDouble(sDiscount3);


            if (txtDisc4.Text == "")
            {
                _disc4 = 0;
            }
            else
            {
                _disc4 = Convert.ToDouble(sDiscount4);
            }

            if (_disc1 > 0)
            {
                _discount1 = (_GrossAmount - _TotalLineDiscount) * (_disc1 / 100);

                _TotalDiscount = (_GrossAmount - _TotalLineDiscount) - _discount1;
            }
            if (_disc2 > 0)
            {
                _discount2 = (_TotalDiscount * (_disc2 / 100));
                _TotalDiscount = _TotalDiscount - _discount2;

            }
            if (_disc3 > 0)
            {
                _discount3 = (_TotalDiscount * (_disc3 / 100));
                _TotalDiscount = _TotalDiscount - _discount3;
            }

            if (_disc4 > 0)
            {
                _discount4 = (_TotalDiscount * (_disc4 / 100));
                _TotalDiscount = _TotalDiscount - _discount4;
            }

            _TotalDiscount = (_discount1 + _discount2 + _discount3 + _discount4);
            txtTotalDiscount.Text = _TotalDiscount.ToString("#,###.00");



            txtGrossAmount.Text = _GrossAmount.ToString("#,###.00");
            txtTotalLineDiscount.Text = _TotalLineDiscount.ToString("#,###.00");

            _NetAmount = (_GrossAmount - _TotalDiscount) - _TotalLineDiscount;

            string _DISCOUNT_METHOD = GetDiscountMethod();
            if (this.VATCode != "NON-VAT")
            {
                if (_DISCOUNT_METHOD == "After")
                {
                    if (this.VATCode == "VAT Inclusive")
                    {
                        _VatAmount = _NetAmount - (_NetAmount / Convert.ToDouble(this.VATRate));
                    }
                    else if (this.VATCode == "VAT Exclusive")
                    {
                        _VatAmount = _NetAmount * Convert.ToDouble(this.VATRate);
                        _NetAmount = _NetAmount + _VatAmount;
                    }
                }
                else
                {
                    if (this.VATCode == "VAT Inclusive")
                    {
                        _VatAmount = _GrossAmount - (_GrossAmount / Convert.ToDouble(this.VATRate));
                    }
                    else if (this.VATCode == "VAT Exclusive")
                    {
                        _VatAmount = _GrossAmount * Convert.ToDouble(this.VATRate);
                        _NetAmount = ((_GrossAmount + _VatAmount) - (_TotalDiscount - _TotalDiscount));
                    }
                }
            }

            txtVAT.Text = _VatAmount.ToString("#,###.00");
            if (_DISCOUNT_METHOD == "After")
            {
                txtNetAmt.Text = _NetAmount.ToString("#,###.00");
            }
            else
            {
                txtNetAmt.Text = ((_GrossAmount + _VatAmount) - (_TotalDiscount + _TotalLineDiscount)).ToString("#,###.00");
            }



            #region Returning Percent Sign
            if (_disc1 > 0)
            {
                txtDisc1.Text = _disc1.ToString("#.00") + "%";
            }
            else
            {
                txtDisc1.Text = "0%";
            }
            if (_disc2 > 0)
            {
                txtDisc2.Text = _disc2.ToString("#.00") + "%";
            }
            else
            {
                txtDisc2.Text = "0%";
            }
            if (_disc3 > 0)
            {
                txtDisc3.Text = _disc3.ToString("#.00") + "%";
            }
            else
            {
                txtDisc3.Text = "0%";
            }

            if (_disc4 > 0)
            {
                txtDisc4.Text = _disc4.ToString("#.00") + "%";
            }
            else
            {
                txtDisc4.Text = "0%";
            }
            #endregion


        }

        void BindWarehouse()
        {
            WarehouseManager oManager = new WarehouseManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetWarehouse();
            cboLocation.Visible = false;
            cboLocation.DataSource = oTable;
            cboLocation.DisplayMember = "Dscription";
            cboLocation.ValueMember = "Code";

            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Code", "Dscription", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboLocation.Refresh();
            cboLocation.Visible = true;
            oManager.Close();
        }

        void RemoveBlankLastRowFirst()
        {
            int iCount = -1;
            gridViewRR.MoveLast();

            int[] oRow = gridViewRR.GetSelectedRows();

            iCount = oRow[0];
            string sItemCode = "";


            sItemCode = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();

            if (sItemCode.Trim() == "")
            {
                gridViewRR.DeleteRow(iCount);
            }


        }

        public void ShowSearch()
        {
            FND_GRPO oSearchDocument = new FND_GRPO();

            oSearchDocument.ConnectionString = this.ConnectionString;
            oSearchDocument.ShowDialog();

            string sDocumentNumber = oSearchDocument.DocumentNumber;


            if (sDocumentNumber != null || sDocumentNumber.Trim() != "")
            {
                GoodsReceivingReportManager oManager = new GoodsReceivingReportManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataRow oRow = oManager.GetGRPO(sDocumentNumber);
             

                #region Header 
                txtDocumentNumber.Text = oRow["DocNum"].ToString();
                txtStatus.Text = oRow["DocStatus"].ToString();
                dtPostDate.EditValue = (DateTime)oRow["PostingDate"];
                dtDeliveryDate.EditValue = (DateTime)oRow["DueDate"];
                dtDocDate.EditValue = (DateTime)oRow["DocDate"];
               
                txtCardCode.EditValue = oRow["CardCode"].ToString();
                AddressBookManager oCardManager = new AddressBookManager();
                oCardManager.Connection = oManager.Connection;
                this.DefaultPriceList = oCardManager.GetPriceListCode(txtCardCode.Text);
                txtCardName.EditValue = oRow["CardName"].ToString();
                txtContactPerson.EditValue = oRow["ContactPerson"].ToString();
                txtReference.EditValue = oRow["Reference"].ToString();
                txtRequestedBy.EditValue = oRow["RequestedBy"].ToString();
                txtDepartment.EditValue = oRow["Department"].ToString();
                txtBillTo.EditValue = oRow["BillTo"].ToString();
                txtShipTo.EditValue = oRow["ShipTo"].ToString();
                
                txtRemarks.EditValue = oRow["Remarks"].ToString();
                txtSuppliersInvoice.Text = oRow["SupplierInvoice"].ToString();
                if (oRow["Location"] != DBNull.Value)
                {
                    cboLocation.SelectedValue = oRow["Location"].ToString();
                }


                if (oRow["VATCode"] != DBNull.Value)
                {
                    this.VATCode = oRow["VATCode"].ToString();
                }

                this.VATRate = oRow["VATRate"].ToString();

                if (oRow["IsVATInclusive"] != DBNull.Value)
                {

                    if (oRow["IsVATInclusive"].ToString() == "Y")
                    {
                        this.IsInclusive = true;
                    }
                    else
                    {
                        this.IsInclusive = false;
                    }
                }

                txtGrossAmount.EditValue = oRow["GrossAmount"].ToString();
                txtVAT.EditValue = oRow["VATAmount"].ToString();
                txtDisc1.EditValue = oRow["Discount1"].ToString();
                txtDisc2.EditValue = oRow["Discount2"].ToString();
                txtDisc3.EditValue = oRow["Discount3"].ToString();
                txtDisc4.EditValue = oRow["Discount4"].ToString();
                txtTotalDiscount.EditValue = oRow["TotalDiscount"].ToString();
                txtNetAmt.EditValue = oRow["NetAmount"].ToString();
                #endregion

                #region Line Items
                this.GRPOLineItems = oManager.GetGRPOLineItems(sDocumentNumber);
                gridRR.DataSource = this.GRPOLineItems;
                gridRR.Refresh();

                #endregion

                #region Distributed Accounts
                this.AccountingEntries = oManager.GetAccountingDistribution(sDocumentNumber);

                gridRRAcctEntries.DataSource = this.AccountingEntries;
                gridRRAcctEntries.Refresh();
                #endregion

                oManager.Close();

                btnAdd.Text = "Update";

                ComputeTotal();
                ApplyVolumeDiscount();

            }

            string _USR_AUTHORIZATION = ApplyAuthorization();
            if (_USR_AUTHORIZATION == "No Access")
            {
                btnCancel_Click(null, null);
            }

        }

        void CancelOrder(string sDocNumber)
        {
            if (MessageBox.Show("Are you sure you want to cancel this document?", "Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                GoodsReceivingReportManager oManager = new GoodsReceivingReportManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                oManager.CancelGRPO(sDocNumber);
                oManager.Close();

                txtStatus.Text = "Canceled";
            }

        }

        void PostRequest(string sDocNumber)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to post this document?", "Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    GoodsReceivingReportManager oManager = new GoodsReceivingReportManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();
                    oManager.PostGRPO(cboLocation.SelectedValue.ToString(), sDocNumber);
                    oManager.Close();

                    txtStatus.Text = "Posted";

                    #region Closing PO

                    PurchaseOrderManager oPOMgr = new PurchaseOrderManager();

                    oPOMgr.ConnectionString = this.ConnectionString;
                    oPOMgr.Open();
                    oPOMgr.SetAppliedAmount(txtReference.Text, txtNetAmt.Text.Replace(",", ""));
                    oPOMgr.Close();

                    #endregion


                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void CloseRequest(string sDocNumber)
        {
            try
            {

                if (MessageBox.Show("Are you sure you want to close this document?", "Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    GoodsReceivingReportManager oManager = new GoodsReceivingReportManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();
                    oManager.CloseGRPO(sDocNumber);
                    oManager.Close();

                    txtStatus.Text = "Closed";

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public int ObjectType { get { return 8; } }


        string ApplyAuthorization()
        {
            UserManager oManager = new UserManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            string iAccessType = oManager.GetAccessType(this.ObjectType, this.ActiveUserID);

            oManager.Close();

            switch (iAccessType)
            {
                case "Full Access":
                    FullAccess();
                    break;
                case "Read Only":
                    ApplyReadOnly();
                    break;
                case "No Access":
                    NoAccess();
                    break;
            }

            return iAccessType;
        }


        int ApplyReadOnly()
        {
            DisableControls();

            txtStatus.Properties.Buttons[0].Enabled = false;
            txtStatus.Properties.Buttons[1].Enabled = false;


            return 2;
        }

        int FullAccess()
        {
            txtStatus.Properties.Buttons[0].Enabled = true;
            txtStatus.Properties.Buttons[1].Enabled = true;

            return 1;
        }

        int NoAccess()
        {
            MessageBox.Show("You have no right to access this module!!!!", "Security Policy", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return 3;

        }



        #region Form Events

        private void GoodsReceivingReport_Load(object sender, EventArgs e)
        {
            dtPostDate.EditValue = System.DateTime.Now;
            dtDeliveryDate.EditValue = System.DateTime.Now;
            dtDocDate.EditValue = System.DateTime.Now;

            BindWarehouse();

            cboLocation.SelectedValue = this.DefaultWarehouse;

            if (this.IsMultipleWarehouse == false)
            {
                cboLocation.Enabled = false;
            }

            #region Details
            if (this.GRPOLineItems == null)
            {
                this.GRPOLineItems = GetTable();
            }

            gridRR.DataSource = this.GRPOLineItems;
            gridRR.Refresh();

            if (gridViewRR.DataRowCount <= 0)
            {
                gridViewRR.AddNewRow();
            }
            #endregion
            
            #region GL Accounts
            if (this.AccountingEntries == null)
            {
                this.AccountingEntries = GetGLAcctStructure();
            }

            gridRRAcctEntries.DataSource = this.AccountingEntries;
            gridRRAcctEntries.Refresh();

            if (gridViewRRAcctEntries.DataRowCount <= 0)
            {
                gridViewRRAcctEntries.AddNewRow();
            }
            #endregion
            InitializeSeries();
            
        }

        private void txtBPCode_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_BP _SearchBP = new FND_BP();
            
            AddressBookManager oBPManager = new AddressBookManager();
            TaxManager oTxManager = new TaxManager();

            try
            {
                _SearchBP.ConnectionString = this.ConnectionString;
                _SearchBP.BusinessPartnerType = "Vendor";
                _SearchBP.ShowDialog();

                txtCardCode.Text = _SearchBP.CardCode;
                txtCardName.Text = _SearchBP.CardName;
                txtBillTo.Text = _SearchBP.Address;
                txtContactPerson.Text = _SearchBP.ContactPerson;
                this.DefaultPriceList = _SearchBP.PriceList;
                this.WithHoldingTaxRate = _SearchBP.WithHoldingTax;
                this.VATCode = _SearchBP.VATCode;
              
                oTxManager.ConnectionString = this.ConnectionString;
                oTxManager.Open();


                this.VATRate = oTxManager.GetVATRate(this.VATCode).ToString();

                this.IsInclusive = oTxManager.IsInclusive(this.VATCode) == "Y" ? true : false;

                oTxManager.Close();


                GetBusinessPartnerDefaults(txtCardCode.Text);

                _SearchBP.Dispose();
            }
            catch
            {
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            string _USR_AUTHORIZATION = ApplyAuthorization();
            if (_USR_AUTHORIZATION == "No Access" || _USR_AUTHORIZATION == "Read Only")
            {
                return;
            }

            try
            {
                int iCount = -1;
                int[] oRow = gridViewRR.GetSelectedRows();

                iCount = oRow[0];

                if (e.KeyCode == Keys.Delete)
                {
                    if (txtStatus.Text == "Draft")
                    {
                        if (MessageBox.Show("Are you sure you want to delete this Row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {

                            if (gridViewRR.IsLastRow)
                            {
                                string sItemCode = "";

                                oRow = gridViewRR.GetSelectedRows();
                                iCount = oRow[0];
                                sItemCode = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();


                                if (sItemCode != "")
                                {
                                    gridViewRR.DeleteRow(iCount);
                                    gridViewRR.AddNewRow();
                                }

                                
                                gridViewRR.RefreshData();

                            }
                            else
                            {

                                string sItemCode = "";


                                oRow = gridViewRR.GetSelectedRows();
                                iCount = oRow[0];
                                sItemCode = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();

                                if (sItemCode != "")
                                {
                                    gridViewRR.DeleteRow(iCount);
                                }

                                gridViewRR.RefreshData();
                            }

                            ComputeTotal();
                        }
                    }
                }
                
                if (e.KeyCode == Keys.Down)
                {
                    if (gridViewRR.IsLastRow)
                    {
                        string sItemCode = "";
                        oRow = gridViewRR.GetSelectedRows();

                        iCount = oRow[0];
                        sItemCode = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();

                        if (sItemCode != "" && (txtStatus.Text == "Draft"))
                        {
                            gridViewRR.AddNewRow();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnSearchItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string _ItemGUID = System.Guid.NewGuid().ToString();
            int iCount = gridViewRR.FocusedRowHandle;

            FND_ITM_PR _FindItem_PUR = new FND_ITM_PR();

            _FindItem_PUR.ConnectionString = this.ConnectionString;
            _FindItem_PUR.ShowDialog();

            if (_FindItem_PUR.ItemCode != null)
            {
                if (_FindItem_PUR.ItemCode.Trim() != "")
                {

                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["ItemCode"], _FindItem_PUR.ItemCode);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["ItemDesc"], _FindItem_PUR.Description);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["UoM"], _FindItem_PUR.UoM);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"], _FindItem_PUR.UnitPrice);
                    
                    if (Convert.ToDouble(this.VATRate) == 0)
                    {
                        gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"], _FindItem_PUR.UnitPrice);
                        
                    }
                    else
                    {

                        gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"], _FindItem_PUR.UnitPrice);
                        
                    }

                    int _ZeroVAL = 0;
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["DiscPrcnt"], _ZeroVAL);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Discount"], _ZeroVAL);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _ZeroVAL);
                    bool IsChecked = false;
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["IsFree"], IsChecked);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["ItemGuid"], _ItemGUID);
                }
                else
                {
                    gridViewRR.CancelUpdateCurrentRow();
                    if (gridViewRR.RowCount <= 0)
                    {
                        gridViewRR.AddNewRow();
                    }
                }
            }
            else
            {
                gridViewRR.CancelUpdateCurrentRow();
                if (gridViewRR.RowCount <= 0)
                {
                    gridViewRR.AddNewRow();
                }
            }

            _FindItem_PUR.Dispose();
        }

        private void btnSearchItemCode_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Enter)
            {
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                try
                {
                    string _ItemGuid = System.Guid.NewGuid().ToString();
                    string sItemCode = oButtonEdit.Text;
                    ItemManager oManager = new ItemManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    DataTable dt = oManager.GetPurItmsByCode(sItemCode);

                    oManager.Close();

                    if (dt.Rows.Count > 0)
                    {
                        #region If Has Rows
                        var items = from row in dt.AsEnumerable()
                                    select new
                                    {
                                        ItemCode = row["ItemCode"].ToString(),
                                        Dscription = row["Dscription"].ToString(),
                                        UoM = row["UoM"].ToString(),
                                        UnitPrice = row["UnitPrice"].ToString()
                                    };


                        int iCount = -1;
                        int[] oRow = gridViewRR.GetSelectedRows();

                        iCount = oRow[0];

                        string sVAT = this.VATCode.Trim() != "" ? this.VATCode : "N/A";
                        double _ZeroVAL = 0;
                        int iRowIndex = gridViewRR.FocusedRowHandle;

                        if (iRowIndex < 0)
                        {
                            iRowIndex = 0;
                        }
                        foreach (var oUnit in items)
                        {
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["ItemCode"], oUnit.ItemCode);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["ItemDesc"], oUnit.Dscription);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["UoM"], oUnit.UoM);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"], oUnit.UnitPrice);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Discount"], _ZeroVAL);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["DiscPrcnt"], _ZeroVAL);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Discount"], _ZeroVAL);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _ZeroVAL);
                            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["ItemGuid"], _ItemGuid);
                        }
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("No item found in the Price List!!!", "Item Code", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        oButtonEdit.Text = "";
                        oButtonEdit.Focus();
                    }
                }
                catch
                {
                }
            }
        }

        private void txtQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    int iCount = -1;
                    int[] oRow = gridViewRR.GetSelectedRows();
                    iCount = oRow[0];

                    DevExpress.XtraEditors.TextEdit oTextQty = (DevExpress.XtraEditors.TextEdit)sender;

                    double _QUANTITY = Convert.ToDouble(oTextQty.Text);
                    string _ITEMCODE = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();
                    double _UNITPRICE = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"]).ToString());
                    double _TOTAL_DISCOUNT = _QUANTITY *  Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["Discount"]).ToString());

                    double _AMOUNT = (_QUANTITY * _UNITPRICE) - _TOTAL_DISCOUNT;
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _TOTAL_DISCOUNT);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Amount"], _AMOUNT);
                    bool IsChecked = false;
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["IsFree"], IsChecked);
                    
                    ComputeTotal();
                    ApplyVolumeDiscount();
                }
                catch
                {
                }
            }
        }

        private void txtLineDiscount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int iCount = -1;
                int[] oRow = gridViewRR.GetSelectedRows();

                iCount = oRow[0];
                DevExpress.XtraEditors.TextEdit oTxtLineDiscount = (DevExpress.XtraEditors.TextEdit)sender;

                double _lineDiscount = Convert.ToDouble(oTxtLineDiscount.Text);
                double _UnitPrice = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"]).ToString());
                double _Qty = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["Qty"]).ToString());
                double _Percentage = 0;

                _Percentage = _lineDiscount / _UnitPrice;

                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["DiscPrcnt"], (_Percentage * 100));
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _Qty * _lineDiscount);

                double _TotalDiscount = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"]).ToString());

                double _Amount = (_Qty * _UnitPrice) - _TotalDiscount;
                
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Amount"], _Amount);

                ComputeTotal();

            }
        }

        private void txtLineDiscPercent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int iCount = -1;
                int[] oRow = gridViewRR.GetSelectedRows();

                iCount = oRow[0];
                DevExpress.XtraEditors.TextEdit oTxtDiscPrcnt = (DevExpress.XtraEditors.TextEdit)sender;

                double _discPrcnt = Convert.ToDouble(oTxtDiscPrcnt.Text);
                double _UnitPrice = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"]).ToString());
                double _Qty = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["Qty"]).ToString());

                double _LineDiscount = _UnitPrice * (_discPrcnt/100);
                double _TotalDiscount = _Qty * _LineDiscount;

                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Discount"], _LineDiscount);
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _TotalDiscount);

                double _Amount = (_Qty * _UnitPrice) - _TotalDiscount;
                
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Amount"], _Amount);

                ComputeTotal();
                
            }
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtSuppliersInvoice.Text.Trim() != "")
            {
                GoodsReceivingReportManager oManager = new GoodsReceivingReportManager();
                GoodsReceivingUnit oUnit = new GoodsReceivingUnit();

                RemoveBlankLastRowFirst();
                if (btnAdd.Text != "Update")
                {
                    #region Header Sales Quote

                    txtDocumentNumber.Text = GetNewSeries();
                    oUnit.DocumentNumber = txtDocumentNumber.Text;
                    oUnit.DocumentStatus = txtStatus.Text;
                    oUnit.PostingDate = (DateTime)dtPostDate.EditValue;
                    oUnit.DueDate = (DateTime)dtDeliveryDate.EditValue;
                    oUnit.DocDate = (DateTime)dtDocDate.EditValue;


                    oUnit.CardCode = txtCardCode.Text;
                    oUnit.CardName = txtCardName.Text;
                    oUnit.ContactPerson = txtContactPerson.Text;
                    oUnit.Reference = txtReference.Text;
                    oUnit.RequestedBy = txtRequestedBy.Text;
                    oUnit.Department = txtDepartment.Text;
                    oUnit.ShipTo = txtShipTo.Text;
                    oUnit.BillTo = txtBillTo.Text;

                    if (cboLocation.SelectedValue != null)
                    {
                        oUnit.Location = cboLocation.SelectedValue.ToString();
                    }
                    else
                    {
                        oUnit.Location = "";
                    }



                    oUnit.Remarks = txtRemarks.Text;
                    oUnit.SupplierInvoice = txtSuppliersInvoice.Text;
                    oUnit.WithHoldingTax = Convert.ToDouble(txtWithHoldingTax.Text);
                    oUnit.TaxCode = this.VATCode.ToString();
                    oUnit.TaxRate = Convert.ToDouble(this.VATRate.ToString());
                    if (oUnit.TaxCode != "NON-VAT")
                    {
                        oUnit.IsVATInclusive = this.IsInclusive == true ? "Y" : "N";
                    }
                    else
                    {
                        oUnit.IsVATInclusive = "N/A";
                    }



                    oUnit.GrossAmnt = Convert.ToDouble(txtGrossAmount.Text.Replace(",", "").ToString());
                    oUnit.VATAmt = Convert.ToDouble(txtVAT.Text.Replace(",", "").ToString());
                    oUnit.TotalLineDisc = Convert.ToDouble(txtTotalLineDiscount.Text.Replace(",", "").ToString());

                    oUnit.Discount1 = txtDisc1.Text;
                    oUnit.Discount2 = txtDisc2.Text;
                    oUnit.Discount3 = txtDisc3.Text;
                    oUnit.Discount4 = txtDisc4.Text;

                    oUnit.TotalDiscount = Convert.ToDouble(txtTotalDiscount.Text.Replace(",", "").ToString());
                    oUnit.NetAmount = Convert.ToDouble(txtNetAmt.Text.Replace(",", "").ToString());
                    //oUnit.WithHoldingTax = Convert.ToDouble(txtWith


                    #endregion

                    oUnit.LineItems = this.GRPOLineItems;

                    #region Adding Part
                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();
                    oManager.AddGRPO(oUnit);
                    oManager.Close();
                    #endregion

                    btnAdd.Text = "Update";

                    MessageBox.Show("Saving document is successful!!!", "Receiving Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
                else
                {

                    #region Header Receiving Report

                    oUnit.DocumentNumber = txtDocumentNumber.Text;
                    oUnit.DocumentStatus = txtStatus.Text;
                    oUnit.PostingDate = (DateTime)dtPostDate.EditValue;
                    oUnit.DueDate = (DateTime)dtDeliveryDate.EditValue;
                    oUnit.DocDate = (DateTime)dtDocDate.EditValue;

                    oUnit.CardCode = txtCardCode.Text;
                    oUnit.CardName = txtCardName.Text;
                    oUnit.ContactPerson = txtContactPerson.Text;
                    oUnit.Reference = txtReference.Text;
                    oUnit.RequestedBy = txtRequestedBy.Text;
                    oUnit.Department = txtDepartment.Text;
                    oUnit.ShipTo = txtShipTo.Text;
                    oUnit.BillTo = txtBillTo.Text;

                    if (cboLocation.SelectedValue != null)
                    {
                        oUnit.Location = cboLocation.SelectedValue.ToString();
                    }
                    else
                    {
                        oUnit.Location = "";
                    }



                    oUnit.Remarks = txtRemarks.Text;
                    oUnit.SupplierInvoice = txtSuppliersInvoice.Text;
                    oUnit.TaxCode = this.VATCode.ToString();
                    oUnit.TaxRate = Convert.ToDouble(this.VATRate.ToString());
                    if (oUnit.TaxCode != "NON-VAT")
                    {
                        oUnit.IsVATInclusive = this.IsInclusive == true ? "Y" : "N";
                    }
                    else
                    {
                        oUnit.IsVATInclusive = "N/A";
                    }

                    oUnit.WithHoldingTax = Convert.ToDouble(txtWithHoldingTax.Text);

                    oUnit.GrossAmnt = Convert.ToDouble(txtGrossAmount.Text.Replace(",", "").ToString());
                    oUnit.VATAmt = Convert.ToDouble(txtVAT.Text.Replace(",", "").ToString());
                    oUnit.TotalLineDisc = Convert.ToDouble(txtTotalLineDiscount.Text.Replace(",", "").ToString());

                    oUnit.Discount1 = txtDisc1.Text;
                    oUnit.Discount2 = txtDisc2.Text;
                    oUnit.Discount3 = txtDisc3.Text;
                    oUnit.Discount4 = txtDisc4.Text;

                    oUnit.TotalDiscount = Convert.ToDouble(txtTotalDiscount.Text.Replace(",", "").ToString());
                    oUnit.NetAmount = Convert.ToDouble(txtNetAmt.Text.Replace(",", "").ToString());

                    #endregion

                    oUnit.LineItems = this.GRPOLineItems;
                    
                    #region Updating Portion
                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();
                    oManager.UpdateGRPO(oUnit);
                    oManager.Close();
                    #endregion

                    MessageBox.Show("Updating document is successful!!!", "Receiving Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
            }
            else
            {
                MessageBox.Show("Please insert Supplier Invoice Reference!!!", "Receiving Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtDiscount_EditValueChanged(object sender, EventArgs e)
        {
            if (txtDisc1.Text.Trim() != "0%" && txtDisc1.Text.Trim() != "" && txtDisc1.Text.Trim() != ".00%" && txtDisc1.Text.Trim() != "0" && txtDisc1.Text.Trim() != ".00")
            {
                txtDisc2.Enabled = true;
            }
            else
            {
                txtDisc2.Enabled = false;
                txtDisc2.Text = "0%";
                txtDisc3.Enabled = false;
                txtDisc3.Text = "0%";
                txtDisc4.Enabled = false;
                txtDisc4.Text = "0%";
            }
        }

        private void txtDisc2_EditValueChanged(object sender, EventArgs e)
        {
            if (txtDisc2.Text.Trim() != "0%" && txtDisc2.Text.Trim() != "" && txtDisc2.Text.Trim() != ".00%" && txtDisc2.Text.Trim() != "0" && txtDisc2.Text.Trim() != ".00")
            {
                txtDisc3.Enabled = true;
            }
            else
            {
                txtDisc3.Enabled = false;
                txtDisc3.Text = "0%";
                txtDisc4.Enabled = false;
                txtDisc4.Text = "0%";
            }
        }

        private void txtDisc3_EditValueChanged(object sender, EventArgs e)
        {
            if (txtDisc3.Text.Trim() != "0%" && txtDisc3.Text.Trim() != "" && txtDisc3.Text.Trim() != ".00%" && txtDisc3.Text.Trim() != "0" && txtDisc3.Text.Trim() != ".00")
            {
                txtDisc4.Enabled = true;
            }
            else
            {
                txtDisc4.Enabled = false;
                txtDisc4.Text = "0%";
            }
        }

        private void txtDisc4_EditValueChanged(object sender, EventArgs e)
        {
            if (txtDisc3.Text.Trim() != "0%" && txtDisc3.Text.Trim() != "" && txtDisc3.Text.Trim() != ".00%" && txtDisc3.Text.Trim() != "0" && txtDisc3.Text.Trim() != ".00")
            {
                txtDisc4.Enabled = true;
            }
            else
            {
                txtDisc4.Enabled = false;
                txtDisc4.Text = "0%";
            }
        }

        private void txtDiscount_Leave(object sender, EventArgs e)
        {
            try
            {
                double iCheck = Convert.ToDouble(txtDisc1.Text.Replace("%", ""));

                if (iCheck > 0)
                {
                    txtDisc1.Text = string.Format("{0:###.00}", txtDisc1.Text.Trim()) + "%";
                }
                else
                {
                    txtDisc1.Text = "0%";
                }
            }
            catch
            {
                MessageBox.Show("Doesn't allow character please insert number only!!!", "Error Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDisc1.Text = "0%";
            }
        }

        private void txtDisc2_Leave(object sender, EventArgs e)
        {
            try
            {
                double iCheck = Convert.ToDouble(txtDisc2.Text.Replace("%", ""));

                if (iCheck > 0)
                {
                    txtDisc2.Text = string.Format("{0:###.00}", txtDisc2.Text.Trim()) + "%";
                }
                else
                {
                    txtDisc2.Text = "0%";
                }
            }
            catch
            {
                MessageBox.Show("Doesn't allow character please insert number only!!!", "Error Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDisc2.Text = "0%";
            }
        }

        private void txtDisc3_Leave(object sender, EventArgs e)
        {
            try
            {
                double iCheck = Convert.ToDouble(txtDisc3.Text.Replace("%", ""));

                if (iCheck > 0)
                {
                    txtDisc3.Text = string.Format("{0:###.00}", txtDisc3.Text.Trim()) + "%";
                }
                else
                {
                    txtDisc3.Text = "0%";
                }
            }
            catch
            {
                MessageBox.Show("Doesn't allow character please insert number only!!!", "Error Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDisc3.Text = "0%";
            }
        }

        private void txtDisc4_Leave(object sender, EventArgs e)
        {
            try
            {
                double iCheck = Convert.ToDouble(txtDisc4.Text.Replace("%", ""));

                if (iCheck > 0)
                {
                    txtDisc4.Text = string.Format("{0:###.00}", txtDisc4.Text.Trim()) + "%";
                }
                else
                {
                    txtDisc4.Text = "0%";
                }
            }
            catch
            {
                MessageBox.Show("Doesn't allow character please insert number only!!!", "Error Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDisc4.Text = "0%";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtQty_Leave(object sender, EventArgs e)
        {
            try
            {
                ComputeTotal();
                ApplyVolumeDiscount();
            }
            catch
            {
            }
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            ComputeTotal();
        }

        private void xtraScrollableControl1_Click(object sender, EventArgs e)
        {

        }

        private void txtDiscount_Enter(object sender, EventArgs e)
        {
            txtDisc1.Text = txtDisc1.Text.Replace("%", "");
        }

        private void txtDisc2_Enter(object sender, EventArgs e)
        {
            txtDisc2.Text = txtDisc2.Text.Replace("%", "");
        }

        private void txtDisc3_Enter(object sender, EventArgs e)
        {
            txtDisc3.Text = txtDisc3.Text.Replace("%", "");
        }

        private void txtDisc4_Enter(object sender, EventArgs e)
        {
            txtDisc4.Text = txtDisc4.Text.Replace("%", "");
        }

        private void txtDiscount_Validated(object sender, EventArgs e)
        {
            ApplyVolumeDiscount();
        }

        private void txtDisc2_Validated(object sender, EventArgs e)
        {
            ApplyVolumeDiscount();
        }

        private void txtDisc3_Validated(object sender, EventArgs e)
        {
            ApplyVolumeDiscount();
        }

        private void txtDisc4_Validated(object sender, EventArgs e)
        {
            ApplyVolumeDiscount();
        }
        
        private void chkFree_CheckedChanged(object sender, EventArgs e)
        {
            int iCount = -1;
            int[] oRow = gridViewRR.GetSelectedRows();
            
            iCount = oRow[0];
            DevExpress.XtraEditors.CheckEdit oCheckBox = (DevExpress.XtraEditors.CheckEdit)sender;

            bool IsChecked = oCheckBox.Checked;
            int _ZeroVAL = 0;
            gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["IsFree"], IsChecked);
            if (IsChecked == true)
            {
                
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["DiscPrcnt"], _ZeroVAL);
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Discount"], _ZeroVAL);
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _ZeroVAL);
                
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Amount"], _ZeroVAL);
            }
            else
            {
                string sItemCode = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();
                double dNetPrice = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"]).ToString());
                double iQty = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["Qty"]).ToString());
                
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["DiscPrcnt"], _ZeroVAL);
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Discount"], _ZeroVAL);
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _ZeroVAL);
                gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Amount"], iQty * Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["UnitPrice"])));
            }

            //OrderLineItems.AcceptChanges();
            ComputeTotal();
            ApplyVolumeDiscount();
        }

        #endregion

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (txtDocumentNumber.Text != "##########" )
            {
                if (txtStatus.Text == "Draft")
                {
                    if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.OK)
                    {
                        PostRequest(txtDocumentNumber.Text);
                    }

                    if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Close)
                    {
                        CancelOrder(txtDocumentNumber.Text);
                    }
                }
                else
                {
                    if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Close)
                    {
                        CloseRequest(txtDocumentNumber.Text);
                    }
                }
            }
            
        }

        private void txtStatus_EditValueChanged(object sender, EventArgs e)
        {
            if (txtStatus.Text == "Draft")
            {
                txtStatus.Properties.Buttons[0].Visible = true;
                txtStatus.Properties.Buttons[1].Visible = true;
                EnableControls();
            }
            if (txtStatus.Text == "Canceled" || txtStatus.Text == "Closed")
            {
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
                DisableControls();
            }

            if (txtStatus.Text == "Posted")
            {
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
                DisableControls();
            }

            if (txtStatus.Text == "Open")
            {
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
                DisableControls();
            }
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    int iCount = -1;
                    int[] oRow = gridViewRR.GetSelectedRows();
                    iCount = oRow[0];

                    DevExpress.XtraEditors.TextEdit oTextPrice = (DevExpress.XtraEditors.TextEdit)sender;

                    
                    string _ITEMCODE = gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["ItemCode"]).ToString();
                    double _QUANTITY = Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["Qty"]).ToString());
                    double _UNITPRICE = Convert.ToDouble(oTextPrice.Text);
                    double _TOTAL_DISCOUNT = _QUANTITY * Convert.ToDouble(gridViewRR.GetRowCellValue(iCount, gridViewRR.Columns["Discount"]).ToString());

                    double _AMOUNT = (_QUANTITY * _UNITPRICE) - _TOTAL_DISCOUNT;
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["TotalLineDisc"], _TOTAL_DISCOUNT);
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["Amount"], _AMOUNT);
                    bool IsChecked = false;
                    gridViewRR.SetRowCellValue(iCount, gridViewRR.Columns["IsFree"], IsChecked);
                    ComputeTotal();
                    ApplyVolumeDiscount();
                }
                catch
                {
                }
            }
        }

        private void txtReference_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_REFERENCE_PO _SEARCH_REFERENCE_RQST = new FND_REFERENCE_PO();

            if (cboLocation.SelectedValue == null)
            {
                MessageBox.Show("Please select warehouse!!!", "Warehouse", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboLocation.Focus();
                return;
            }
            if (txtCardCode.Text != "")
            {
                _SEARCH_REFERENCE_RQST.ConnectionString = this.ConnectionString;
                _SEARCH_REFERENCE_RQST.CardCode = txtCardCode.Text;

                _SEARCH_REFERENCE_RQST.ShowDialog();
                if (_SEARCH_REFERENCE_RQST.DocumentNumber != null)
                {
                    txtReference.Text = _SEARCH_REFERENCE_RQST.DocumentNumber;
                    txtDepartment.Text = _SEARCH_REFERENCE_RQST.Department;
                    txtRequestedBy.Text = _SEARCH_REFERENCE_RQST.RequestedBy;
                    cboLocation.SelectedValue = _SEARCH_REFERENCE_RQST.WhsCode;

                    PurchaseOrderManager oManager = new PurchaseOrderManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();
                    DataTable _TABLE = oManager.GetPOLineItems(txtReference.Text);

                    if (_TABLE.Rows.Count > 0)
                    {
                        this.GRPOLineItems = _TABLE;

                        gridRR.DataSource = this.GRPOLineItems;
                        gridRR.Refresh();
                        oManager.Close();
                    }

                    ComputeTotal();
                }


            }
            else { MessageBox.Show("Please select customer first!!!", "Card Code", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
        }

        private void txtReference_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtCardCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AddressBookManager oManager = new AddressBookManager();
                TaxManager oTxManager = new TaxManager();
                DataTable _TABLE = new DataTable();

                try
                {

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    _TABLE = oManager.GetByBPCode(txtCardCode.Text, "Vendor");

                    oManager.Close();

                    int iCount = _TABLE.Rows.Count;

                    if (iCount == 0)
                    {
                        #region if no data return during enter
                        FND_BP _SearchBP = new FND_BP();

                        AddressBookManager oBPManager = new AddressBookManager();

                        try
                        {
                            _SearchBP.ConnectionString = this.ConnectionString;
                            _SearchBP.BusinessPartnerType = "Vendor";
                            _SearchBP.SetSearchByBPCode(txtCardCode.Text);

                            _SearchBP.ShowDialog();

                            this.WithHoldingTaxRate = _SearchBP.WithHoldingTax;
                            txtCardCode.Text = _SearchBP.CardCode;
                            txtCardName.Text = _SearchBP.CardName;
                            txtBillTo.Text = _SearchBP.Address;
                            txtPaymentTerms.Text = _SearchBP.PaymentTerm.ToString();
                            txtContactPerson.Text = _SearchBP.ContactPerson;

                            this.WithHoldingTaxRate = _SearchBP.WithHoldingTax;
                            this.DefaultPriceList = _SearchBP.PriceList;
                            this.VATCode = _SearchBP.VATCode;
                            try
                            {
                                txtDisc1.Text = _SearchBP.Discount1.ToString() + "%";
                                txtDisc2.Text = _SearchBP.Discount2.ToString() + "%";
                                txtDisc3.Text = _SearchBP.Discount3.ToString() + "%";
                                txtDisc4.Text = _SearchBP.Discount4.ToString() + "%";
                            }
                            catch
                            {
                            }


                            oTxManager.ConnectionString = this.ConnectionString;
                            oTxManager.Open();


                            this.VATRate = oTxManager.GetVATRate(this.VATCode).ToString();

                            this.IsInclusive = oTxManager.IsInclusive(this.VATCode) == "Y" ? true : false;



                            oTxManager.Close();

                     
                            _SearchBP.Dispose();
                        }
                        catch
                        {
                        }
                        #endregion
                        return;
                    }

                    DataRow oRow = _TABLE.Rows[0];

                    this.WithHoldingTaxRate = Convert.ToDouble(oRow["WithHoldingTax"]);
                    txtCardCode.Text = oRow["BPCode"].ToString();
                    txtCardName.Text = oRow["BPName"].ToString();
                    txtBillTo.Text = oRow["Address"].ToString();
                    txtPaymentTerms.Text = oRow["PaymentTerm"].ToString();
                    txtContactPerson.Text = oRow["ContactPerson"].ToString(); ;
                    this.DefaultPriceList = oRow["PriceList"].ToString(); ;
                    this.VATCode = oRow["VATCode"].ToString();
                    try
                    {
                        txtDisc1.Text = oRow["Disc1"].ToString() + "%";
                        txtDisc2.Text = oRow["Disc2"].ToString() + "%";
                        txtDisc3.Text = oRow["Disc3"].ToString() + "%";
                        txtDisc4.Text = oRow["Disc4"].ToString() + "%";
                    }
                    catch
                    {
                    }

                    //cboCurrency.Text 
                    oTxManager.ConnectionString = this.ConnectionString;
                    oTxManager.Open();


                    this.VATRate = oTxManager.GetVATRate(this.VATCode).ToString();

                    this.IsInclusive = oTxManager.IsInclusive(this.VATCode) == "Y" ? true : false;


                    oTxManager.Close();
                    GetBusinessPartnerDefaults(txtCardCode.Text);
                    //if (oRow["SMCODE"].ToString() != "")
                    //{
                    //    cboSalesman.EditValue = oRow["SMCODE"].ToString();
                    //}
                }
                catch
                {
                }
            }
        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "")

                e.DisplayText = (e.RowHandle + 1).ToString();
        }

        private void GoodsReceiptPO_Shown(object sender, EventArgs e)
        {
            gridRR.Select();
            gridViewRR.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
            gridViewRR.FocusedColumn = gridViewRR.Columns["ItemCode"];
            gridViewRR.ShowEditor();


            txtCardCode.Focus();

            string _USR_AUTHORIZATION = ApplyAuthorization();
            if (_USR_AUTHORIZATION == "No Access")
            {
                btnCancel_Click(sender, e);
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void barAddNewEntry_ItemClick(object sender, ItemClickEventArgs e)
        {
            int iRowIndex = gridViewRRAcctEntries.RowCount;
            if (gridViewRRAcctEntries.RowCount <= 0)
            {
                gridViewRRAcctEntries.AddNewRow();
                return;
            }
            gridViewRRAcctEntries.FocusedRowHandle = iRowIndex - 1;
            try
            {

                string _AcctCode = "";


                if (gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]) != null)
                {
                    _AcctCode = gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]).ToString();
                }
                else
                {
                    _AcctCode = "";
                }

                if (_AcctCode.Trim() == "")
                {
                    return;
                }
            }
            catch { }

            gridViewRRAcctEntries.AddNewRow();
        }

        private void barSaveARDistribution_ItemClick(object sender, ItemClickEventArgs e)
        {
            GoodsReceivingReportManager oManager = new GoodsReceivingReportManager();

            RemoveBlankLastGLAccounts();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            SqlException ex = oManager.AddDistributionEntries(this.txtDocumentNumber.Text, this.AccountingEntries);

            if (ex != null)
            {
                MessageBox.Show(ex.Message.ToString(), "Error Saving", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("Accounting is successfully saved!!!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            oManager.Close();
        }

        private void barClear_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.AccountingEntries = GetGLAcctStructure();

            gridRRAcctEntries.DataSource = this.AccountingEntries;
            gridRRAcctEntries.Refresh();
        }

        private void txtAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;

            oForm.ShowDialog();

            gridViewRRAcctEntries.SetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"], oForm.SelectedGLAcctCode);
            gridViewRRAcctEntries.SetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctName"], oForm.SelectedGLAcctName);

        }

        private void gridViewRRAcctEntries_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]).ToString().Trim() != "")
                {
                    if (MessageBox.Show("Are you sure you want to delete this item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {

                        gridViewRRAcctEntries.DeleteRow(gridViewRRAcctEntries.FocusedRowHandle);
                    }
                }
            }

            if (e.KeyCode == Keys.Down)
            {
                int iRowIndex = gridViewRRAcctEntries.FocusedRowHandle;
                if (gridViewRRAcctEntries.RowCount <= 0)
                {
                    gridViewRRAcctEntries.AddNewRow();
                    return;
                }
                gridViewRRAcctEntries.FocusedRowHandle = iRowIndex;

                if (gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]).ToString().Trim() != "" || gridViewRRAcctEntries.GetRowCellValue(gridViewRRAcctEntries.FocusedRowHandle, gridViewRRAcctEntries.Columns["AcctCode"]) != null)
                {
                    gridViewRRAcctEntries.AddNewRow();
                    return;
                }

            }
        }

        private void GoodsReceivingReport_Resize(object sender, EventArgs e)
        {
            try
            {
                txtDocumentNumber.Left = this.Width - 180;
                txtStatus.Left = this.Width - 180;
                dtPostDate.Left = this.Width - 180;
                dtDeliveryDate.Left = this.Width - 180;
                dtDocDate.Left = this.Width - 180;
                txtSuppliersInvoice.Left = this.Width - 180;
                txtGrossAmount.Left = this.Width - 376;
                txtTotalLineDiscount.Left = this.Width - 376;
                txtTotalDiscount.Left = this.Width - 159;
                txtWithHoldingTax.Left = this.Width - 159;
                txtNetAmt.Left = this.Width - 376;
                txtVAT.Left = this.Width - 376;

                txtDisc4.Left = this.Width - txtTotalDiscount.Width - 107;
                txtDisc3.Left = this.Width - txtTotalDiscount.Width - txtDisc4.Width - 113;
                txtDisc2.Left = this.Width - txtTotalDiscount.Width - txtDisc4.Width - txtDisc3.Width - 119;
                txtDisc1.Left = this.Width - txtTotalDiscount.Width - txtDisc4.Width - txtDisc3.Width - txtDisc2.Width - 122;

                lblDocStatus.Left = this.Width - 280;
                lblDocNo.Left = this.Width - 280;
                lblPostDate.Left = this.Width - 280;
                lblValidUntilDeliveryDate.Left = this.Width - 280;
                lblDocumentDate.Left = this.Width - 280;
                lblSuppliersInvoice.Left = this.Width - 280;

                
                lblTotalBefDiscount.Left = this.Width - txtGrossAmount.Width - 160;
                lblTotalLineDiscount.Left = this.Width - txtTotalLineDiscount.Width - 160;
                lblDiscount.Left = this.Width - txtTotalLineDiscount.Width - 160;
                lblVAT.Left = this.Width - txtTotalLineDiscount.Width - 160;
                lblNetAmt.Left = this.Width - txtTotalLineDiscount.Width - 160;
                lblWithHoldingTax.Left = this.Width - txtWithHoldingTax.Width - 102;

                gridRR.Width = this.Width - 58;

            }
            catch
            {
            }
        }

        

        
    }
}
